package com.baseDemo.web.service.role.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baseDemo.web.dao.authority.AuthorityEntityDao;
import com.baseDemo.web.dao.navigation.NavigationEntityDao;
import com.baseDemo.web.dao.role.RoleEntityDao;
import com.baseDemo.web.entity.AuthorityEntity;
import com.baseDemo.web.entity.NavigationEntity;
import com.baseDemo.web.entity.RoleEntity;
import com.baseDemo.web.entity.UserEntity;
import com.baseDemo.web.service.role.RoleService;
import com.share.hibernate.baseDaoSupport.PageUtil;
import com.share.hibernate.baseDaoSupport.TreeUtil;
import com.share.service.baseServiceSupport.impl.BaseServiceSupport;
@Service
public class RoleServiceImpl extends BaseServiceSupport<RoleEntity> implements
		RoleService {
	@Resource
	RoleEntityDao roleEntityDao;
	@Resource AuthorityEntityDao authorityEntityDao;
	@Resource NavigationEntityDao navigationEntityDao;
	@Resource PageUtil pageUtil;
	
	@Override
	public PageUtil getListForPageToJson(Integer start, Integer limit) {
		Integer rowTotal = roleEntityDao.getPageTotal();
		pageUtil.setTotal(rowTotal);
		List<RoleEntity> list = roleEntityDao.find(limit, start);
		pageUtil.setEntityList(list);
		return pageUtil;
	}

	@Override
	public String updateRole(RoleEntity roleEntity) {
		return null;
	}

	@Override
	public Set<UserEntity> getUsers(String roleId) {
		RoleEntity roleEntity = roleEntityDao.getById(roleId);
		if(roleEntity!=null){
			return roleEntity.getUsers();
		}
		return null;
	}

	@Override
	public String addUsersForRole(String roleId, Set<UserEntity> userEntities) {
		RoleEntity roleEntity = roleEntityDao.getById(roleId);
		if(roleEntity!=null){
			roleEntity.getUsers().addAll(userEntities);
			return String.valueOf(1);
		}
		return String.valueOf(0);
	}

	@Override
	public String deleteUsersForRole(String roleId, Set<UserEntity> userEntities) {
		RoleEntity roleEntity = roleEntityDao.getById(roleId);
		if(roleEntity!=null){
			Set<UserEntity> tmp = new HashSet<UserEntity>();
			Set<UserEntity> users = roleEntity.getUsers();
			for (UserEntity user : users) {
				for (UserEntity userEntity : userEntities) {
					if(user.equals(userEntity)){
						tmp.add(user);
					}
				}
			}
			users.removeAll(tmp);
			return String.valueOf(1);
		}
		return String.valueOf(0);
	}

	@Override
	public List<TreeUtil> getAuthorityList() {
		List<NavigationEntity> apps = this.navigationEntityDao.getNavByLeafFlase();
		if(apps!=null&&apps.size()>0){
			List<TreeUtil> treeList = new ArrayList<TreeUtil>();
			for (NavigationEntity app : apps) {
				TreeUtil tree = new TreeUtil(app.getId(),app.getText(),app.getIcon(),true,false,false);
				Set<AuthorityEntity> authorities = app.getAuthorities();
				if(authorities!=null&&authorities.size()>0){
					Set<TreeUtil> children = new HashSet<TreeUtil>();
					for (AuthorityEntity authority : authorities) {
						TreeUtil childTree = new TreeUtil(authority.getId(),authority.getAuthorityDescription(),"",true,true,false);
						children.add(childTree);
					}
					tree.setChildren(children);
					treeList.add(tree);
				}
			}
			return treeList;
		}
		return null;
	}

	@Override
	public String updateAuthority(List<String> autid, String roleId) {
		RoleEntity roleEntity = roleEntityDao.getById(roleId);
		if(roleEntity!=null){
			Set<AuthorityEntity> authorities = roleEntity.getAuthorities();
			authorities.clear();
			for (String id : autid) {
				AuthorityEntity authority = authorityEntityDao.getById(id);
				if(authority!=null){
					authorities.add(authority);
				}
			}
			return String.valueOf(1);
		}
		return String.valueOf(0);
	}

	@Override
	public Set<AuthorityEntity> getAuthoritiesByRole(String roleId) {
		RoleEntity role = roleEntityDao.getById(roleId);
		return role.getAuthorities();
	}
}
